﻿package com.firewolf.codecounter;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

public class FileUtil {

	/**
	 * 查询某个目录及其子目录下后缀为end的所有文件
	 * @param dir
	 * @param end 
	 * @return
	 */
	public static ArrayList<File> getAllFilesWithSuffix(File dir,String end){
		FileFilterWithFolder filter = new FileFilterWithFolder(end);
		File[] listFiles = dir.listFiles(filter);
		ArrayList<File> files = new ArrayList<File>();
		if(listFiles != null && listFiles.length > 0){
			for(File file : listFiles){
				if(!file.isDirectory())
					files.add(file);
				else{
					files.addAll(getAllFilesWithSuffix(file, end));
				}
			}
		}
		return files;
	}
}

/**
 * 文件过滤器，
 * @author Administrator
 *
 */
class FileFilterWithFolder implements FileFilter{
	private String end = null;
	
	public FileFilterWithFolder(String end) {
		this.end = end;
	}
	
	@Override
	public boolean accept(File pathname) {
		return pathname.getName().toLowerCase().endsWith(end) || pathname.isDirectory();
	}
	
}